package ar.com.ctrln.view.render.compras.proveedor;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.EventQueue;
import org.zkoss.zk.ui.event.EventQueues;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Image;
import org.zkoss.zul.Label;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;
import org.zkoss.zul.Toolbarbutton;

import ar.com.ctrln.persistence.model.proveedor.Proveedor;

public class ConsultarProveedorRowRender implements RowRenderer<Proveedor> {
	
	private Proveedor prov;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
   	public void render(Row row, Proveedor data, int int2) throws Exception {
		
		if (data.getFechaBaja()!=null)
			row.setVisible(false);
		Toolbarbutton buttonWeb = new Toolbarbutton(data.getSitioWeb());
    	buttonWeb.addEventListener("onClick",new EventListener() {
      		 public void onEvent(Event event) throws Exception {
      			 String url = ((Proveedor)((Row)event.getTarget().getParent()).getAttribute("data")).getSitioWeb();
      			 Executions.getCurrent().sendRedirect("http://"+url, "_blank");
     	   }
  	  });	
    	row.appendChild(new Label(data.getRazonSocial()));
    	if(data.getTipoServicios()!=null){
    	row.appendChild(new Label(data.getTipoServicios().iterator().next().getNombre()));
    	}else{
    		row.appendChild(new Label("No asignado"));
    	}
    	row.appendChild(new Label(data.getEmail()));
    	row.appendChild(new Label(data.getTelefonos().iterator().next().getNumero()));
    	row.appendChild(buttonWeb);

        Hbox box = new Hbox();
        box.setSpacing("12px");
        box.setWidth("100%");
        box.setPack("center");
        Image img1 = new Image();
        Image img2 = new Image();
        Image img3 = new Image();
       
        img1.setSrc("/resources/icons/verTabla.png");
		img2.setSrc("/resources/icons/editarTabla.png");
        img3.setSrc("/resources/icons/bajaTabla.png");
     
        img1.setTooltiptext("Ver detalle del Proveedor");
        img2.setTooltiptext("Editar Proveedor");
        img3.setTooltiptext("Dar de baja Proveedor");
        img1.addEventListener("onClick", new VerProveedorListener());
        img2.addEventListener("onClick", new EditarProveedorListener());
        img3.addEventListener("onClick", new darBajaListener());

        img1.setParent(box);
        img2.setParent(box);
        img3.setParent(box); 
        img1.setWidth("24px");
        img1.setHeight("24px");
        img2.setWidth("24px");
        img2.setHeight("24px");
        img3.setWidth("24px");
        img3.setHeight("24px");
        box.setParent(row);
        row.setAttribute("data", data);
    }     

	@SuppressWarnings("rawtypes")
	class darBajaListener implements org.zkoss.zk.ui.event.EventListener {
		
		private EventQueue eqA = (EventQueue) EventQueues.lookup("refreshProveedores",EventQueues.DESKTOP, true);
				
		@SuppressWarnings("unchecked")
		@Override
		public void onEvent(Event evt) throws Exception {
			prov = (Proveedor) ((Row) evt.getTarget().getParent().getParent()).getAttribute("data");			
			prov.setFechaBaja(new Date());
			eqA.publish(new Event("onButtonClick", null,prov));

		}
	}
	
@SuppressWarnings("rawtypes")
class VerProveedorListener implements org.zkoss.zk.ui.event.EventListener {
		@Override
		public void onEvent(Event evt) throws Exception {
			Proveedor prov = (Proveedor) ((Row) evt.getTarget().getParent().getParent()).getAttribute("data");			
			Map<String, Proveedor> args = new HashMap<String, Proveedor>();
			args.put("data", prov);
			Executions.createComponents("/zul/compras/proveedor/verProveedor.zul",null
					, args);

		}
	}

@SuppressWarnings("rawtypes")
class EditarProveedorListener implements org.zkoss.zk.ui.event.EventListener {
		@Override
		public void onEvent(Event evt) throws Exception {
			Proveedor prov = (Proveedor) ((Row) evt.getTarget().getParent().getParent()).getAttribute("data");			
			Map<String, Proveedor> args = new HashMap<String, Proveedor>();
			args.put("data", prov);
			Executions.createComponents("/zul/compras/proveedor/editarProveedor.zul",null
					, args);

		}
	}
    
    

}
